/*
 * @Author: zcwhy 12345678
 * @Date: 2023-12-13 15:51:07
 * @LastEditors: zcwhy 12345678
 * @LastEditTime: 2023-12-20 17:06:24
 * @FilePath: \xxProject\src\store\modules\user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { login, logout, getInfo,openlogin } from '@/api/login.ts';
import { getToken, setToken, removeToken } from '@/utils/auth';
import { defineStore } from 'pinia';
import { userInfo, addInfo } from '@/api/system/user.ts'

const useUserStore = defineStore('user', {
    state: (): {
        token?: string;
        name: string;
        userId: number,
        avatar: string;
        roles: any[];
        permissions: string[];
        wx: boolean,
        info: Object,
    } => ({
        token: getToken(),
        name: '',
        userId: 0,
        avatar: '',
        roles: [],
        permissions: [],
        wx: false,
        info: {}
    }),
    actions: {
        // 登录
        // 获取用户信息
        getInfo() {
            return new Promise((resolve, reject) => {

                openlogin({ openid: 'o5k2H5XcD4h3PL_unjgjB-opmOsA' }).then((item: any) => {
                    setToken(item.token)
                
              
                getInfo()
                    .then((res: any) => {
                        const user = res.user;
                        this.name = user.userName;
                        this.userId = user.userId

                        //javascript判断是否是微信访问
                        var ua = navigator.userAgent.toLowerCase();
                        if (ua.match(/MicroMessenger/i) == "micromessenger") {
                            this.wx = true;
                        } else {
                            this.wx = false;

                        }
                        userInfo({ userId: useUserStore().userId }).then((arr: any) => {
                            if (arr.data) {
                                console.log(arr.data, "测试")
                                this.info = arr.data
                                resolve(res);
                            } else {
                                let data = {
                                    name: useUserStore().name,
                                    userId: useUserStore().userId,
                                    aggressivity: '40',
                                    HP: '300',
                                    defense: '45',
                                    Mana: '300',
                                    speed: '20',
                                    realm: '1', //境界
                                    experience: '0',
                                    need: '400',
                                    bodys: '1',
                                    fortune: '30',
                                    Skills: '101',
                                    SkillsInfo1: "101",
                                    SkillsInfo2: "104",
                                    SkillsInfo3: "105",
                                    SkillsInfo4: "102",
                                }
                                this.info = data
                                addInfo(data).then((arr: any) => {
                                    resolve(res);
                                })

                            }
                        })

                    })
                    .catch(error => {
                        reject(error);
                    });

                })
            });
        },
        // 退出系统
        logOut() {
            return new Promise((resolve, reject) => {
                logout()
                    .then(() => {
                        this.token = '';
                        this.roles = [];
                        this.permissions = [];
                        removeToken();
                        resolve(1);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
    },
});

export default useUserStore;
